set(MV OFF)
set(PIP_INSTALL_OPTIONS "")

if("${PIP_INSTALL_PREFIX}" STREQUAL "--user")
    set(PIP_INSTALL_OPTIONS "--user")
elseif(NOT "${PIP_INSTALL_PREFIX}" STREQUAL "")
    set(PIP_INSTALL_OPTIONS "--prefix=${PIP_INSTALL_PREFIX}")
    if(NOT "@USE_EXACT_LIB_SUBPATH@" STREQUAL "")
        set(MV ON)
    endif()
endif()

execute_process(
    COMMAND @PIP_PYTHON_EXECUTABLE@ -m pip install --upgrade --force-reinstall --no-deps --no-cache-dir --no-index ${PIP_INSTALL_OPTIONS} --find-links=@PYTHON_PACKAGE_DIR@/dist opentera_webrtc.@PYTHON_SUBPACKAGE_NAME@
    WORKING_DIRECTORY @PYTHON_PACKAGE_DIR@
)

if(${MV} STREQUAL "ON")
    execute_process(
        # The format is the same between --user (where pip should be installed) and --prefix (where the package is installed)
        COMMAND bash -c "rsync -a --remove-source-files ${PIP_INSTALL_PREFIX}/lib/@USE_EXACT_LIB_SUBPATH_SOURCE@/ ${PIP_INSTALL_PREFIX}/lib/@USE_EXACT_LIB_SUBPATH@/ 2> /dev/null"
        WORKING_DIRECTORY @PYTHON_PACKAGE_DIR@
    )
    execute_process(
        COMMAND find ${PIP_INSTALL_PREFIX}/lib -depth -type d -empty -delete
        WORKING_DIRECTORY @PYTHON_PACKAGE_DIR@
    )
endif()

execute_process(
    COMMAND @CMAKE_COMMAND@ -E touch @WORKING_DIR@/install.stamp
    WORKING_DIRECTORY @PYTHON_PACKAGE_DIR@
)
